/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * tambahPegawaiForm.java
 *
 * Created on 24 Apr 12, 10:36:17
 */
package com.DataPegawai;

import com.DaftarPegawai.daftarPegawaiControl;
import com.DaftarPegawai.daftarPegawaiForm;
import com.entities.Pegawai;
import com.entities.pegawaiTableModel;
import java.sql.Date;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 *
 * @author Ignacio
 */
public class tambahPegawaiForm extends javax.swing.JDialog {

    JTable tabelPeg;

    /** Creates new form tambahPegawaiForm */
    public tambahPegawaiForm(int statusUpdate) {
        initComponents();
        this.setSize(1015, 640);
        tambahPegawaiControl.setForm(this);
        if (statusUpdate == 1) {
            List<Pegawai> get = Pegawai.getPegs();
            nipText.setText(get.get(0).getNIP());
            namaText.setText(get.get(0).getNama());
            tempatText.setText(get.get(0).getTempatLahir());
            String tanggalLahir = get.get(0).getTanggalLahir();
            int tahun = Integer.parseInt(tanggalLahir.substring(0, 4));
            int bulan = Integer.parseInt(tanggalLahir.substring(5, 7));
            int tanggal = Integer.parseInt(tanggalLahir.substring(8));
            tglLahirChooser.setDate(new Date(tahun, bulan, tanggal));
            if (get.get(0).getJenisKelamin().equals("laki-laki")) {
                lakiRadio.setSelected(true);
            } else {
                perempuanRadio.setSelected(true);
            }
            alamatTextArea.setText(get.get(0).getAlamat());
            pdidikCombo.setSelectedItem(get.get(0).getPendidikan());
            String tanggalKerja = get.get(0).getTanggalKerja();
            int tahunK = Integer.parseInt(tanggalKerja.substring(0, 4));
            int bulanK = Integer.parseInt(tanggalKerja.substring(5, 7));
            int tanggalK = Integer.parseInt(tanggalKerja.substring(8));
            tglKerjaChooser.setDate(new Date(tahunK, bulanK, tanggalK));
            tipeCombo.setSelectedItem(get.get(0).getTipePegawai());
            String gol = get.get(0).getGolongan();
            if (gol.length() == 7) {
                tingkatGolCombo.setSelectedIndex(0);
                kelasGolCombo.setSelectedIndex(0);
                tipeCombo.setSelectedItem("Honorer");
            } else if (gol.length() == 4) {
                String tingkat = gol.substring(0, 3);
                String kelas = gol.substring(-1);
                tingkatGolCombo.setSelectedItem(tingkat);
                kelasGolCombo.setSelectedItem(kelas);
                tipeCombo.setSelectedItem("PNS");
            } else if (gol.length() == 3) {
                String tingkat = gol.substring(0, 2);
                String kelas = gol.substring(-1);
                tingkatGolCombo.setSelectedItem(tingkat);
                kelasGolCombo.setSelectedItem(kelas);
                tipeCombo.setSelectedItem("PNS");
            } else {
                String tingkat = gol.substring(0, 1);
                String kelas = gol.substring(1);
                tingkatGolCombo.setSelectedItem(tingkat);
                kelasGolCombo.setSelectedItem(kelas);
                tipeCombo.setSelectedItem("PNS");
            }
            MKGtext.setText(get.get(0).getMKG() + "");
            jabatanText.setText(get.get(0).getJabatan());
            nipText.setEnabled(false);
            lakiRadio.setEnabled(false);
            perempuanRadio.setEnabled(false);
            saveButton.setText("Update");
            saveButton.setName("Update");

        }
//        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        panelAwal = new javax.swing.JPanel();
        headerPanel = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        mainPanel = new javax.swing.JPanel();
        nipText = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        namaText = new javax.swing.JTextField();
        tglLahirChooser = new com.toedter.calendar.JDateChooser();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        tipeCombo = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        tempatText = new javax.swing.JTextField();
        kelasGolCombo = new javax.swing.JComboBox();
        tingkatGolCombo = new javax.swing.JComboBox();
        pdidikCombo = new javax.swing.JComboBox();
        cancelButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        tglKerjaChooser = new com.toedter.calendar.JDateChooser();
        jLabel11 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        alamatTextArea = new javax.swing.JTextArea();
        jabatanText = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        lakiRadio = new javax.swing.JRadioButton();
        perempuanRadio = new javax.swing.JRadioButton();
        MKGtext = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Sistem Kepegawaian Puskesmas Jebed - Form Pegawai Baru"); // NOI18N
        setResizable(false);

        panelAwal.setBackground(new java.awt.Color(255, 255, 255));

        headerPanel.setBackground(new java.awt.Color(153, 255, 204));

        jLabel10.setBackground(new java.awt.Color(51, 255, 204));
        jLabel10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/imagePKM.png"))); // NOI18N

        javax.swing.GroupLayout headerPanelLayout = new javax.swing.GroupLayout(headerPanel);
        headerPanel.setLayout(headerPanelLayout);
        headerPanelLayout.setHorizontalGroup(
            headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(headerPanelLayout.createSequentialGroup()
                .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, 989, Short.MAX_VALUE)
                .addContainerGap())
        );
        headerPanelLayout.setVerticalGroup(
            headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
        );

        mainPanel.setBackground(new java.awt.Color(255, 255, 255));
        mainPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Data Pegawai"));
        mainPanel.setLayout(null);

        nipText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nipTextActionPerformed(evt);
            }
        });
        mainPanel.add(nipText);
        nipText.setBounds(280, 20, 150, 20);

        jLabel1.setText("Tanggal Lahir"); // NOI18N
        mainPanel.add(jLabel1);
        jLabel1.setBounds(440, 80, 110, 20);

        jLabel2.setText("NIP"); // NOI18N
        mainPanel.add(jLabel2);
        jLabel2.setBounds(64, 21, 200, 20);

        jLabel3.setText("Nama"); // NOI18N
        mainPanel.add(jLabel3);
        jLabel3.setBounds(64, 51, 200, 20);

        jLabel4.setText("Golongan"); // NOI18N
        mainPanel.add(jLabel4);
        jLabel4.setBounds(64, 342, 180, 20);
        mainPanel.add(namaText);
        namaText.setBounds(280, 50, 150, 20);
        mainPanel.add(tglLahirChooser);
        tglLahirChooser.setBounds(560, 80, 110, 20);

        jLabel5.setText("Tempat Lahir"); // NOI18N
        mainPanel.add(jLabel5);
        jLabel5.setBounds(64, 81, 200, 20);

        jLabel6.setText("Jenis Kelamin"); // NOI18N
        mainPanel.add(jLabel6);
        jLabel6.setBounds(64, 111, 190, 20);

        jLabel7.setText("Pendidikan"); // NOI18N
        mainPanel.add(jLabel7);
        jLabel7.setBounds(64, 253, 190, 20);

        jLabel8.setText("Tanggal Mulai Kerja"); // NOI18N
        mainPanel.add(jLabel8);
        jLabel8.setBounds(64, 282, 210, 20);

        tipeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-Tipe-", "Honorer", "PNS" }));
        tipeCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tipeComboActionPerformed(evt);
            }
        });
        mainPanel.add(tipeCombo);
        tipeCombo.setBounds(280, 310, 150, 20);

        jLabel9.setText("Tipe Pegawai"); // NOI18N
        mainPanel.add(jLabel9);
        jLabel9.setBounds(64, 312, 180, 20);
        mainPanel.add(tempatText);
        tempatText.setBounds(280, 80, 150, 20);

        kelasGolCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "a", "b", "c", "d", "e" }));
        mainPanel.add(kelasGolCombo);
        kelasGolCombo.setBounds(330, 340, 40, 20);

        tingkatGolCombo.setFont(new java.awt.Font("Times New Roman", 0, 11));
        tingkatGolCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "I", "II", "III", "IV" }));
        mainPanel.add(tingkatGolCombo);
        tingkatGolCombo.setBounds(280, 340, 40, 19);

        pdidikCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-Pilih Pendidikan-", "SMA", "S1", "S2", "D3" }));
        mainPanel.add(pdidikCombo);
        pdidikCombo.setBounds(280, 250, 150, 20);

        cancelButton.setText("Cancel"); // NOI18N
        cancelButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        mainPanel.add(cancelButton);
        cancelButton.setBounds(280, 420, 90, 23);

        saveButton.setText("Save"); // NOI18N
        saveButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });
        mainPanel.add(saveButton);
        saveButton.setBounds(410, 420, 90, 23);
        mainPanel.add(tglKerjaChooser);
        tglKerjaChooser.setBounds(280, 280, 150, 20);

        jLabel11.setText("Alamat"); // NOI18N
        mainPanel.add(jLabel11);
        jLabel11.setBounds(64, 142, 119, 20);

        alamatTextArea.setColumns(20);
        alamatTextArea.setRows(5);
        jScrollPane1.setViewportView(alamatTextArea);

        mainPanel.add(jScrollPane1);
        jScrollPane1.setBounds(280, 140, 235, 96);
        mainPanel.add(jabatanText);
        jabatanText.setBounds(280, 370, 150, 20);

        jLabel12.setText("Jabatan"); // NOI18N
        mainPanel.add(jLabel12);
        jLabel12.setBounds(64, 373, 180, 20);

        buttonGroup1.add(lakiRadio);
        lakiRadio.setText("Laki-Laki"); // NOI18N
        lakiRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lakiRadioActionPerformed(evt);
            }
        });
        mainPanel.add(lakiRadio);
        lakiRadio.setBounds(280, 110, 100, 23);

        buttonGroup1.add(perempuanRadio);
        perempuanRadio.setText("Perempuan"); // NOI18N
        perempuanRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                perempuanRadioActionPerformed(evt);
            }
        });
        mainPanel.add(perempuanRadio);
        perempuanRadio.setBounds(400, 110, 110, 23);
        mainPanel.add(MKGtext);
        MKGtext.setBounds(480, 340, 30, 20);

        jLabel13.setText("Masa Kerja"); // NOI18N
        mainPanel.add(jLabel13);
        jLabel13.setBounds(380, 340, 100, 20);

        jLabel14.setText("Tahun"); // NOI18N
        mainPanel.add(jLabel14);
        jLabel14.setBounds(520, 340, 50, 20);

        javax.swing.GroupLayout panelAwalLayout = new javax.swing.GroupLayout(panelAwal);
        panelAwal.setLayout(panelAwalLayout);
        panelAwalLayout.setHorizontalGroup(
            panelAwalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(headerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(panelAwalLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 786, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(203, 203, 203))
        );
        panelAwalLayout.setVerticalGroup(
            panelAwalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelAwalLayout.createSequentialGroup()
                .addComponent(headerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelAwal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelAwal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-1015)/2, (screenSize.height-686)/2, 1015, 686);
    }// </editor-fold>//GEN-END:initComponents

    private void nipTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nipTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nipTextActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed

        // TODO add your handling code here:
        if (saveButton.getText().equals("Save")) {
            try {
                int status = tambahPegawaiControl.simpanPegawaiAction(nipText, namaText, tempatText,
                        lakiRadio, perempuanRadio, alamatTextArea, tglLahirChooser, tglKerjaChooser,
                        pdidikCombo, tipeCombo, tingkatGolCombo, kelasGolCombo, jabatanText, MKGtext);
                if (status == 1) {
                    JOptionPane.showMessageDialog(null, "Data pegawai berhasil ditambahkan.");
                    nipText.setText(null);
                    namaText.setText(null);
                    tempatText.setText(null);
                    tglLahirChooser.setDate(new GregorianCalendar().getTime());
                    lakiRadio.setSelected(false);
                    perempuanRadio.setSelected(false);
                    pdidikCombo.setSelectedIndex(0);
                    alamatTextArea.setText(null);
                    tglKerjaChooser.setDate(new GregorianCalendar().getTime());
                    tipeCombo.setSelectedIndex(0);
                    tingkatGolCombo.setSelectedIndex(0);
                    kelasGolCombo.setSelectedIndex(0);
                    MKGtext.setText(null);
                    jabatanText.setText(null);
                } else {
                    JOptionPane.showMessageDialog(null, "Gagal menambahkan pegawai !", "Warning!", 2);
                }
            } catch (SQLException ex) {
                Logger.getLogger(tambahPegawaiControl.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Gagal menambahkan/mengubah pegawai !", "Warning!", 2);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(tambahPegawaiControl.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Gagal menambahkan/mengubah pegawai !", "Warning!", 2);
            }
        } else if(saveButton.getText().equals("Update")){
            try {
                int status = tambahPegawaiControl.updatePegawaiAction(nipText, namaText, tempatText,
                        lakiRadio, perempuanRadio, alamatTextArea, tglLahirChooser, tglKerjaChooser,
                        pdidikCombo, tipeCombo, tingkatGolCombo, kelasGolCombo, jabatanText, MKGtext);
                if (status == 1) {
                    JOptionPane.showMessageDialog(null, "Data pegawai berhasil diubah.");
                    nipText.setText(null);
                    namaText.setText(null);
                    tempatText.setText(null);
                    tglLahirChooser.setDate(new GregorianCalendar().getTime());
                    lakiRadio.setSelected(false);
                    perempuanRadio.setSelected(false);
                    pdidikCombo.setSelectedIndex(0);
                    alamatTextArea.setText(null);
                    tglKerjaChooser.setDate(new GregorianCalendar().getTime());
                    tipeCombo.setSelectedIndex(0);
                    tingkatGolCombo.setSelectedIndex(0);
                    kelasGolCombo.setSelectedIndex(0);
                    MKGtext.setText(null);
                    jabatanText.setText(null);
                } else {
                    JOptionPane.showMessageDialog(null, "Gagal mengubah pegawai !", "Warning!", 2);
                }
            } catch (SQLException ex) {
                Logger.getLogger(tambahPegawaiControl.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Gagal menambahkan/mengubah pegawai !", "Warning!", 2);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(tambahPegawaiControl.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Gagal menambahkan/mengubah pegawai !", "Warning!", 2);
            }
        }

    }//GEN-LAST:event_saveButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        daftarPegawaiControl.getFrame().show();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void lakiRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lakiRadioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_lakiRadioActionPerformed

    private void perempuanRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_perempuanRadioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_perempuanRadioActionPerformed

    private void tipeComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tipeComboActionPerformed
        // TODO add your handling code here:
        if (tipeCombo.getSelectedItem().equals("Honorer")) {
            tingkatGolCombo.setSelectedItem("");
            kelasGolCombo.setSelectedItem("");
            tingkatGolCombo.setEnabled(false);
            kelasGolCombo.setEnabled(false);
            MKGtext.setText("0");
            MKGtext.setEditable(false);
            jabatanText.requestFocus();
        } else if (tipeCombo.getSelectedItem().equals("PNS")) {
            tingkatGolCombo.setEnabled(true);
            kelasGolCombo.setEnabled(true);
        }
    }//GEN-LAST:event_tipeComboActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField MKGtext;
    private javax.swing.JTextArea alamatTextArea;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel headerPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jabatanText;
    private javax.swing.JComboBox kelasGolCombo;
    private javax.swing.JRadioButton lakiRadio;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JTextField namaText;
    private javax.swing.JTextField nipText;
    private javax.swing.JPanel panelAwal;
    private javax.swing.JComboBox pdidikCombo;
    private javax.swing.JRadioButton perempuanRadio;
    private javax.swing.JButton saveButton;
    private javax.swing.JTextField tempatText;
    private com.toedter.calendar.JDateChooser tglKerjaChooser;
    private com.toedter.calendar.JDateChooser tglLahirChooser;
    private javax.swing.JComboBox tingkatGolCombo;
    private javax.swing.JComboBox tipeCombo;
    // End of variables declaration//GEN-END:variables
}
